package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.projo.Student;
import com.xiaohai.mapper.StudentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.projo.dto.StudentDto;
import com.xiaohai.service.ClassService;
import com.xiaohai.service.MajorService;
import com.xiaohai.service.ScoreService;
import com.xiaohai.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohai
 * @since 2024-01-22
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private MajorService majorService;

    @Autowired
    private ClassService classService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public boolean add(Student student) {
        boolean save = super.save(student);
        return save;
    }

    @Override
    public boolean updateInfo(Student student) {
        return studentMapper.updateAccount(student);
    }

    @Override
    public boolean updatePasswordByAccount(String account, String newPassword) {
        int update = studentMapper.updatePasswordByAccount(account, newPassword);
        return update > 0;
    }

    @Override
    public Page<StudentDto> mylist(Integer currentPage, Integer pageSize, String studentName, String majorName, String className, Integer identity, String year,String account) {
        Page<StudentDto> page = new Page<>(currentPage, pageSize);
        Page<StudentDto> list1 = null;
        if (identity.equals(1)) {// 管理员
            list1 = studentMapper.list(page, studentName, majorName, className, year);
        }
        if (identity.equals(3)) {// 教师
            list1 = studentMapper.listByTea(page, studentName, majorName,account,year);
        }
        return list1;
    }

    @Override
    public StudentDto detail(String id) {
        StudentDto studentDto = new StudentDto();

        Student student = super.getById(id);
        // 查询专业
        Integer majorId = student.getMajorId();
        String name = majorService.getById(majorId).getName();
        studentDto.setMajorName(name);


        // 查询班级
        Integer classId = student.getClassId();
        String name1 = classService.getById(classId).getName();
        studentDto.setClassName(name1);

        BeanUtils.copyProperties(student, studentDto);
        return studentDto;
    }

    @Override
    public Boolean myUpdate(Student student) {
        boolean b = super.updateById(student);
        return b;
    }

    @Override
    public boolean delete(Integer id) {
        Boolean b1 = scoreService.isEmptyByStudentId(id);
        if (b1) {
            boolean b = super.removeById(id);
            return b;
        }
        return false;
    }

    @Override
    public Boolean batchAdd(List<Student> students) {
        boolean batch = super.saveBatch(students);
        return batch;
    }

    @Override
    public Boolean isEmptyByClassId(Integer id) {
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(id != null, Student::getClassId, id);
        long a = super.count(studentLambdaQueryWrapper);
        return a == 0;
    }

    @Override
    public List<HashMap<String, Integer>> getMenWomen() {
        return studentMapper.getMenWomen();
    }


    @Override
    public List<HashMap<String, Integer>> list1(Integer identity, String name,String account) {
        List<HashMap<String, Integer>> maps = null;
        switch (identity) {
            case 1:
                maps = studentMapper.list1();
                break;
            case 2:
                maps = studentMapper.listByStu(account, name);
                break;
            case 3:
                maps = studentMapper.listByEchartTea(account,name);
                break;
        }
        return maps;
    }


}
